package reflect.xml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class test1 {
    public static void main(String[] args) {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        DocumentBuilder db= null;
        try {
            db = dbf.newDocumentBuilder();
            Document doc=db.parse("src/reflect/xml/收藏信息.xml");
            NodeList brandList=doc.getElementsByTagName("Brand");
            //循环Brand信息
            for(int i=0;i<brandList.getLength();i++){
                Node brand=brandList.item(i);
                Element element=(Element)brand;
                String attrValue=element.getAttribute("name");
                NodeList types=element.getChildNodes();

                for(int j=0;j<types.getLength();j++){
                    Element typeElement=((Element)types.item(j));
                    String type=typeElement.getAttribute("name");
                    System.out.println("手机："+attrValue+type);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
